package top.lunarye.handler.pending;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import top.lunarye.common.domaim.TaskInfo;
import top.lunarye.handler.deduplication.DeduplicationRuleService;
import top.lunarye.handler.deduplication.service.DeduplicationService;
import top.lunarye.handler.handler.HandlerHolder;

import javax.annotation.Resource;

/**
 * Task执行器
 * 1. 去重
 * 2. 发送消息
 * @author lunar
 * @since 2022-07-18
 */
@Data
@Accessors(chain = true)
@Slf4j
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class Task implements Runnable{

    @Resource
    private HandlerHolder handlerHolder;

    @Resource
    private DeduplicationRuleService deduplicationRuleService;

    private TaskInfo taskInfo;

    @Override
    public void run() {
        // 1. 去重
        deduplicationRuleService.duplication(taskInfo);

        // 2，发送
        if (CollUtil.isNotEmpty(taskInfo.getReceiver())) {
            handlerHolder.route(taskInfo.getSendChannel()).doHandler(taskInfo);
        }
    }
}
